/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.passport.service.plugins  
 */
package com.apache.passport.service.plugins;

import java.util.ArrayList;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.common.DesUtils;
import com.apache.passport.common.PassportHelper;
import com.apache.passport.entity.Token;
import com.apache.passport.entity.UctUser;
import com.apache.passport.manager.UctUserManager;

/**
 * description:  更新用户手机号
 * 此操作必须为注册用户<br>
 * 需要传递参数有tokenId,userEname,mobile,ptsidg
 * 需要调用PassportHelper.getInstance().SHA1(ArrayList<String> lst)对数据进行加密处理
 * @author Hou Dayu 创建时间：2015-3-3  
 * @Copyright(c)2014:
 */
public class UpdateMobilePlugin implements PluginConnector {
	protected IDao uctUserDao;
	private UctUserManager uctUserManager;

	public void setUctUserDao(IDao uctUserDao) {
		this.uctUserDao = uctUserDao;
	}

	public void setUctUserManager(UctUserManager uctUserManager) {
		this.uctUserManager = uctUserManager;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo paramsvo) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("true");
		String tokenId = (String) paramsvo.getParams("tokenId");
		String userEname = (String) paramsvo.getParams("userEname");

		String mobile = (String) paramsvo.getParams("mobile");
		String ptsidg = (String) paramsvo.getParams("ptsidg");
		ArrayList<String> lst = new ArrayList<String>();
		lst.add(tokenId);
		lst.add(userEname);
		lst.add(mobile);
		String macPtsidg = PassportHelper.getInstance().SHA1(lst);
		Token token = (Token) SystemTools.getInstance().getCache("loginToken").getCacheCloneByKey(tokenId);

		if (Validator.isEmpty(ptsidg)) {
			result.setEntity("F");
			result.setMessage("校验参数缺失");
			return result;
		}

		if (!ptsidg.equals(macPtsidg)) {
			result.setEntity("F");
			result.setMessage("系统检测到信息被篡改");
			return result;
		}

		//登录之后才可修改
		if (Validator.isEmpty(token)) {
			result.setEntity("F");
			result.setMessage("用户未登录或认证已过期");
			return result;
		}

		if (Validator.isNull(userEname)) {
			result.setEntity("F");
			result.setMessage("用户名不能为空");
			return result;
		}

		String desEname = DesUtils.getInstance().decrypt(token.getUserEname());
		if (!userEname.equals(desEname)) {
			result.setEntity("F");
			result.setMessage("用户名不一致");
			return result;
		}

		if (Validator.isNull(mobile)) {
			result.setEntity("F");
			result.setMessage("手机号不能为空");
			return result;
		}

		UctUser uctUser = (UctUser) getInfoByMobile(mobile);
		if (!Validator.isEmpty(uctUser)) {
			result.setEntity("F");
			result.setMessage("手机号已存在");
			return result;
		}

		UctUser obj = (UctUser) getInfoByEname(userEname);
		if (Validator.isEmpty(obj)) {
			result.setEntity("F");
			result.setMessage("用户不存在,请确认!");
			return result;
		}
		obj.setMobile(mobile);
		paramsvo.setObj(obj);
		uctUserManager.editInfo(paramsvo);
		result.setEntity("T");
		result.setMessage("手机号修改成功");
		return result;
	}

	/**
	 * description:  根据ename查询用户信息
	 * @param vo
	 * @return  
	 * @author Hou Dayu 
	 * @param userEname 
	 * @update 2014-12-19
	 */
	public Object getInfoByEname(String userEname) {
		String sql = uctUserDao.getSql(3);
		MethodParam param = new MethodParam("ByUserEname", "", sql, "com.apache.passport.entity.UctUser");
		param.setParams("userEname", userEname);
		return uctUserDao.selectSingle(param);
	}

	/**
	 * description:  根据手机号查询用户信息
	 * @param mobile
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-19
	 */
	public Object getInfoByMobile(String mobile) {

		String sql = uctUserDao.getSql(5);
		MethodParam param = new MethodParam("ByMobile", "", sql, "com.apache.passport.entity.UctUser");
		param.setParams("mobile", mobile);
		return uctUserDao.selectSingle(param);
	}

	public IDao getUctUserDao() {
		return uctUserDao;
	}

	public UctUserManager getUctUserManager() {
		return uctUserManager;
	}

}
